import axios from 'axios';

// 登录请求方法
const loginreq = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        data: params,
        traditional: true,
        transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => {
        console.info(res.data)
        res.data.success = res.data.code == '200'
        res.data.msg = res.data.message
        return res.data
    });
}

// 通用公用方法 application/x-www-form-urlencoded application/json
const req = (method, url, params) => {
    if ('get' == method || 'GET' == method) {
        return axios({
            method: method,
            url: url,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                accesstoken: localStorage.getItem('logintoken')
            },
            params: params,
            traditional: true,
            transformRequest: [
                function(data) {
                    let ret = ''
                    for (let it in data) {
                        ret +=
                            encodeURIComponent(it) +
                            '=' +
                            encodeURIComponent(data[it]) +
                            '&'
                    }
                    return ret
                }
            ]
        }).then(res => {
            res.data.success = res.data.code == '200'
            res.data.msg = res.data.message
            if (res.data.code == '401') {
                localStorage.removeItem("userInfo")
            }
            return res.data
        });
    }
    
    if ('post' == method || 'POST' == method) {
        return axios({
            method: method,
            url: url,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                accesstoken: localStorage.getItem('logintoken')
            },
            data: params,
            traditional: true,
            transformRequest: [
                function(data) {
                    let ret = ''
                    for (let it in data) {
                        ret +=
                            encodeURIComponent(it) +
                            '=' +
                            encodeURIComponent(data[it]) +
                            '&'
                    }
                    return ret
                }
            ]
        }).then(res => {
            res.data.success = res.data.code == '200'
            res.data.msg = res.data.message
            if (res.data.code == '401') {
                localStorage.removeItem("userInfo")
            }
            return res.data
        });
    }
    
}

// 通用公用方法 application/x-www-form-urlencoded application/json
const reqjson = (method, url, params) => {
    let dataParams;
    if ('get' == method || 'GET' == method) {
        return axios({
            method: method,
            url: url,
            headers: {
                'Content-Type': 'application/json',
                accesstoken: localStorage.getItem('logintoken')
            },
            params: params
        }).then(res => {
            res.data.success = res.data.code == '200'
            res.data.msg = res.data.message
            if (res.data.code == '401') {
                localStorage.removeItem("userInfo")
            }
            return res.data
        });
    }
    if ('post' == method || 'POST' == method) {
        return axios({
            method: method,
            url: url,
            headers: {
                'Content-Type': 'application/json',
                accesstoken: localStorage.getItem('logintoken')
            },
            data: params
        }).then(res => {
            res.data.success = res.data.code == '200'
            res.data.msg = res.data.message
            if (res.data.code == '401') {
                localStorage.removeItem("userInfo")
            }
            return res.data
        });
    }
    
};

export {
    loginreq,
    req,
    reqjson
}